/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import cn.hutool.core.map.MapUtil;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.UserChatOrder;
import me.zhengjie.modules.system.domain.WxUser;
import me.zhengjie.modules.system.service.UserChatOrderService;
import me.zhengjie.modules.system.service.WxUserService;
import me.zhengjie.modules.system.service.dto.AtmrDto;
import me.zhengjie.modules.system.service.dto.ExamOrderDto;
import me.zhengjie.modules.system.service.dto.UserChatOrderDto;
import me.zhengjie.modules.system.service.dto.UserChatOrderQueryCriteria;
import me.zhengjie.utils.StringUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author 王宇
* @date 2024-04-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "聊天订单管理")
@RequestMapping("/api/userChatOrder")
public class UserChatOrderController {

    private final UserChatOrderService userChatOrderService;

    private final WxUserService wxUserService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('userChatOrder:list')")
    public void download(HttpServletResponse response, UserChatOrderQueryCriteria criteria) throws IOException {
        userChatOrderService.download(userChatOrderService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询聊天订单")
    @ApiOperation("查询聊天订单")
    @PreAuthorize("@el.check('userChatOrder:list')")
    public ResponseEntity<Object> query(UserChatOrderQueryCriteria criteria, Pageable pageable){
        List<WxUser> wxUsers = null;
        boolean openId = false;
        if (StringUtils.isNotBlank(criteria.getMobile())) {
            openId = true;
            wxUsers = wxUserService.findByMoblie(criteria.getMobile());
        }
        //前端传递了姓名或电话号码参数
        if(openId)
        {
            if (null != wxUsers && wxUsers.size() > 0) {
                criteria.setOpenId(wxUsers.get(0).getOpenId());
            }
            else
            {
                criteria.setOpenId("-1");
            }
        }
        Map<String,Object> map = userChatOrderService.queryAll(criteria,pageable);
        if (MapUtil.isNotEmpty(map) && map.get("content") != null) {
            List<UserChatOrderDto> contentList = (List<UserChatOrderDto>) map.get("content");
            contentList.parallelStream().forEach(s -> {
                if (StringUtils.isNotBlank(s.getOpenId())) {
                    WxUser user = wxUserService.findByOpenId(s.getOpenId());
                    if (null != user) {
                        s.setMobile(user.getMobile());
                    }
                }
            });
        }
        return new ResponseEntity<>(map,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增聊天订单")
    @ApiOperation("新增聊天订单")
    @PreAuthorize("@el.check('userChatOrder:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody UserChatOrder resources){
        return new ResponseEntity<>(userChatOrderService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改聊天订单")
    @ApiOperation("修改聊天订单")
    @PreAuthorize("@el.check('userChatOrder:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody UserChatOrder resources){
        userChatOrderService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除聊天订单")
    @ApiOperation("删除聊天订单")
    @PreAuthorize("@el.check('userChatOrder:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        userChatOrderService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}